package com.mlt.back.dao.impl;

import com.mlt.back.beans.Brand;
import com.mlt.back.dao.BrandDao;
import com.mlt.back.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BrandDaoImpl implements BrandDao {
    @Override
    public List<Brand> selectAll() {
        List<Brand> brands=new ArrayList<>();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn= DBUtil.getConnection();
            String sql="select brand_id,brand_name,brand_logo,brand_desc,create_time,brand_status from t_brand";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                Brand brand = new Brand();
                brand.setBrand_id(rs.getInt("brand_id"));
                brand.setBrand_name(rs.getString("brand_name"));
                brand.setBrand_logo(rs.getString("brand_logo"));
                brand.setBrand_desc(rs.getString("brand_desc"));
                brand.setCreate_time(rs.getDate("create_time"));
                brand.setBrand_status(rs.getString("brand_status"));
                brands.add(brand);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return brands;
    }

    @Override
    public List<Brand> selectByCategoryId(String category_id) {
        List<Brand> brands=new ArrayList<>();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn= DBUtil.getConnection();
            String sql="select brand_id,brand_name,brand_logo,brand_desc,create_time,brand_status from t_brand join t_category_brand on t_brand.brand_id=t_category_brand.fk_brand_id where t_category_brand.fk_category_id=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,category_id);
            rs=ps.executeQuery();
            while (rs.next()){
                Brand brand = new Brand();
                brand.setBrand_id(rs.getInt("brand_id"));
                brand.setBrand_name(rs.getString("brand_name"));
                brand.setBrand_logo(rs.getString("brand_logo"));
                brand.setBrand_desc(rs.getString("brand_desc"));
                brand.setCreate_time(rs.getDate("create_time"));
                brand.setBrand_status(rs.getString("brand_status"));
                brands.add(brand);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return brands;
    }

    @Override
    public int delById(String brand_id) {
        int count=0;
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            conn=DBUtil.getConnection();
            String sql="delete from t_brand where brand_id=?";
            ps= conn.prepareStatement(sql);
            ps.setString(1,brand_id);
            count=ps.executeUpdate();
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public void changeStatus(String brand_id,String status) {
        int count=0;
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            conn=DBUtil.getConnection();
            String sql="update t_brand set brand_status=? where brand_id=?;";
            ps= conn.prepareStatement(sql);
            ps.setString(1,status);
            ps.setString(2,brand_id);
            count=ps.executeUpdate();
            System.out.println("BrandDao:changeStatus"+count);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
    }

    @Override
    public Brand selectById(String brand_id) {
        Brand brand=new Brand();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn= DBUtil.getConnection();
            String sql="select brand_name,brand_logo,brand_desc,create_time,brand_status from t_brand where brand_id=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,brand_id);
            rs=ps.executeQuery();
            if(rs.next()){
                brand.setBrand_id(Integer.valueOf(brand_id));
                brand.setBrand_name(rs.getString("brand_name"));
                brand.setBrand_logo(rs.getString("brand_logo"));
                brand.setBrand_desc(rs.getString("brand_desc"));
                brand.setCreate_time(rs.getDate("create_time"));
                brand.setBrand_status(rs.getString("brand_status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return brand;
    }

    @Override
    public void doUpdate(Brand brand) {
        Connection conn =null;
        PreparedStatement ps=null;
        try {
            conn=DBUtil.getConnection();
            String sql="update t_brand set brand_name=?,brand_logo=?,brand_desc=?,brand_status=? where brand_id=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, brand.getBrand_name());
            ps.setString(2, brand.getBrand_logo());
            ps.setString(3, brand.getBrand_desc());
            ps.setString(4, brand.getBrand_status());
            ps.setInt(5,brand.getBrand_id());
            int i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
    }
}
